-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Change codegen of LLVM intrinsics to be name-based, and add llvm linkage support for bf16(xN)
and i1xN
#140763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_codegen_ssa |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some changes occurred in compiler/rustc_codegen_gcc |
This comment has been minimized.
This comment has been minimized.
x86amx
for i32x256
for AMX intrinsics
x86amx
for i32x256
for AMX intrinsicsx86amx
and i32x256
for AMX intrinsics
This comment has been minimized.
This comment has been minimized.
I think you can use |
That can be used to improve performance, I am not really focusing on performance in this PR. I want to currently emphasize the correctness of the codegen. |
Oh wait, I probably misunderstood your comment, you meant using the llvm declaration by itself. Yeah, that would be better, thanks for the info. I will update the impl when I get the chance |
I think you can just focus on non-overloaded functions for this PR. Overloaded functions and type checking that checking Rust function signatures using LLVM defined can be subsequent PRs. @rustbot author |
Reminder, once the PR becomes ready for a review, use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@sayantn Taking the address of an intrinsic is invalid LLVM IR. |
Do you have some examples? Most likely the intrinsics aren't invalid, just old. Intrinsics change over time and support auto-upgrade. |
I am testing for upgrades with
Along with that I have found that 2 intrinsics use wrong parameters. The 2nd parameter for Also, there are (invalid) uses of I have also found a lot of upgradable intrinsics (mainly in x86 and arm), but I am not bothered about them. These were not detected before due to
cc @Amanieu |
Some changes occurred in compiler/rustc_codegen_llvm/src/builder/autodiff.rs cc @ZuseZ4 |
This comment has been minimized.
This comment has been minimized.
This PR modifies cc @jieyouxu |
There was an invalid intrinsic in |
After CI is green, I think we should do a try build and an rustc-perf run, because this modifies quite a bit in the codegen of rust and llvm intrinsics, which might have some performance footprint (I am especially concerned about rust-intrinsics, I got rid of the signature table on grounds that we don't need to do it anymore, but we might still need it as a cache) |
x86amx
x86amx
, bf16(xN)
and i1xN
This comment has been minimized.
This comment has been minimized.
Can anyone help here? IDK what's going on, in my system (x86_64-unknown-linux-gnu), I can actually see the |
Assuming they can stand separately, can we split up the intrinsic changes from the |
@workingjubilee If you mean I should separate the implementation changes of Rust intrinsics into another PR, yeah no problem, but I feel like they go with this PR. I can try separating them into clean commits, but separating them into different PRs will imo not be good. But I don't think I should separate the loosening of ABI check rules for PS: I don't really like that we use the same term for rustc builtins and LLVM builtins, but oh well |
- Remove redundant bitcasts at callsite edit (squash with struct)
Sorry for the repeated force pushes, I refactored my code quite a bit, and restructured the commits |
- Correct usage of invalid intrinsics in tests
As @workingjubilee suggested I will split this PR into 2, this will only add bypasses for |
x86amx
, bf16(xN)
and i1xN
bf16(xN)
and i1xN
This PR changes how LLVM intrinsics are codegen
Explanation of the changes
Current procedure
This is the same for all functions, LLVM intrinsics are not treated specially
f32 (f32)
due to the Rust signaturePros
Cons
-Zverify-llvm-ir
to it will fail compilation). I would expect this code to not compile at all instead of generating invalid IR.x86amx
type, and (almost) all intrinsics that have vectors ofi1
types) can't be linked to at all. This is a (major?) roadblock in the AMX and AVX512 support in stdarch.-Zverify-llvm-ir
won't complain. Eventually it will error out due to the non-existing function (courtesy of the linker). I don't think this is a behavior we want.What this PR does
LLVMIntrinsicGetType
to directly get the function type of the intrinsic from LLVM.Regardless, the undermentioned functionalities work for all intrinsics
AutoUpgrade
d by LLVM. If not, that means it is an invalid intrinsic, and we error out.Pros
It is now not possible (or at least, it would require significantly more leaps and bounds) to introduce invalid IR using non-overloaded LLVM intrinsics.
As we are now doing the matching of Rust signatures to LLVM intrinsics ourselves, we can now add bypasses to enable linking to such non-Rust types (e.g. matching 8192-bit vectors to
x86amx
and injectingllvm.x86.cast.vector.to.tile
andllvm.x86.cast.tile.to.vector
s in callsite)This PR adds bypasses for
bf16
(viai16
),bf16xN
(viai16xN
),i1xN
(viaiM
, whereM
is the smallest power of 2 s.t.M >= N
, unlessN <= 4
, where we useM = 8
). This will unblock AVX512-VP2INTERSECT and a lot of bf16 intrinsics in stdarch. This PR also automatically destructures structs if the types don't exactly match (this is required for us to start emitting hard errors on mismmatches).Cons
Possible ways to extend this to overloaded intrinsics (future)
Parse the mangled intrinsic name to get the type parameters
LLVM has a stable mangling of intrinsic names with type parameters (in
LLVMIntrinsicCopyOverloadedName2
), so we can parse the name to get the type parameters, and then just do the same thing.Pros
Cons
TargetExt
types or identified structs, their name is a part of the mangling, making it impossible to reverse. Even more complexities arise when there are unnamed identified structs, as LLVM adds more mangling to the names.Use the
IITDescriptor
table and the Rust function signatureWe can use the base name to get the
IITDescriptor
s of the corresponding intrinsic, and then manually implement the matching logic based on the Rust signature.Pros
TargetExt
types. Also, fun fact, Rust exports all struct types as literal structs (unless it is emitting LLVM IR, then it always uses named identified structs, with mangled names)Cons
llvm.sqrt.bf16
until we havebf16
types in Rust. Because if we are usingu16
s (or any other type) asbf16
s, then the matcher will deduce that the signature isu16 (u16)
notbf16 (bf16)
(which would lead to an error becauseu16
is not a valid type parameter forllvm.sqrt
), even though the intended type parameter is specified in the name.IITDescriptorKind
sThese 2 approaches might give different results for same function. Let's take
The name-based approach will decide that the type parameter is
bf16
, and the LLVM signature isi1 (bf16)
and will inject some bitcasts at callsite.The
IITDescriptor
-based approach will decide that the LLVM signature isi1 (u16)
, and will see that the name given doesn't match the expected name (llvm.is.constant.u16
), and will error out.Other things that this PR does
disables all ABI checks only for the(moved to another PR)unadjusted
ABI to facilitate the implementation of AMX (otherwise passing 8192-bit vectors to the intrinsic won't be allowed). This is "safe" because this ABI is only used to link to LLVM intrinsics, and passing vectors of any lengths to LLVM intrinsics is fine, because they don't exist in machine level.bitcast
s incg_llvm/builder::check_call
(now renamed ascast_arguments
due to its new counterpartcast_return
). This was old code from when Rust used to pass non-erased lifetimes to LLVM.Reviews are welcome, as this is my first time actually contributing to
rustc
After CI is green, we would need a try build and a rustc-perf run.
@rustbot label T-compiler A-codegen A-LLVM
r? codegen